using UnityEngine;
using UnityEngine.UI;

namespace CGF
{
    public class MainLaunch : MonoBehaviour
    {
        public static MainLaunch instance;

        public Text txtVersion;
        public Text txtProgress;
        public Text txtError;
        public Slider slider;
        public RectTransform iconWait;
        public float iconSpeed = 300;

        void Start()
        {
            Debug.Log("Game Start");

            instance = this;
            
            AndroidStatusBarHelper.SetStatusBarTransparent(AndroidStatusBarHelper.StatusTextStyle.Light);

            if (txtVersion)
            {
                if (Define.IsWebGL && !Application.isEditor)
                {
                    txtVersion.gameObject.SetActive(false);
                }
                else
                {
                    txtVersion.text = $"v {CGFConfig.Instance.VersionFullName}";
                }
            }

            ShowWait();

            StartLoadConfigAndRes();
        }

        public void Update()
        {
            if (iconWait && iconWait.gameObject.activeInHierarchy)
            {
                iconWait.Rotate(0, 0, -iconSpeed * Time.deltaTime);
            }
        }

        private async void StartLoadConfigAndRes()
        {
            //检查Apk重下载
            await AppUpdate.CheckDownloadApp();

            if (!Application.isEditor)
            {
                await IndexedDBHelper.Init();
            }

            //加载AB资源
            while (true)
            {
                bool success = await ResManager.Instance.DownloadAutoUpdateABs(progress =>
                {
                    SetProgress(progress);
                });
                if (!success)
                {
                    ShowErrorText("Initialize configs failed");
                    await CTask.Wait(3);
                    ShowWait();
                }
                else
                {
                    break;
                }
            }

            //启动框架
            CGFramework.Instance.Run();
        }

        public void SetProgress(float progress)
        {
            if (!slider.gameObject.activeSelf)
            {
                slider.gameObject.SetActive(true);
                //if (iconWait) iconWait.gameObject.SetActive(false);
                if (txtError) txtError.gameObject.SetActive(false);
            }

            if (txtProgress)
            {
                if (!txtProgress.gameObject.activeSelf) txtProgress.gameObject.SetActive(true);
                txtProgress.text = $"LOADING {Mathf.RoundToInt(progress * 100)}%";
            }

            slider.value = progress;
        }

        public void ShowWait()
        {
            slider.gameObject.SetActive(false);
            if (iconWait) iconWait.gameObject.SetActive(true);
            if (txtProgress) txtProgress.gameObject.SetActive(false);
            if (txtError) txtError.gameObject.SetActive(false);
        }

        public void ShowErrorText(string content)
        {
            slider.gameObject.SetActive(false);
            //if (iconWait) iconWait.gameObject.SetActive(false);
            if (txtError)
            {
                txtError.gameObject.SetActive(true);
                txtError.text = content;
            }
            Debug.Log($"SetTip:{content}");
        }

        private void OnDestroy()
        {
            Debug.Log("MainLaunch被删除");
        }

        // 该方法在游戏首页UI生成后的Awake里调用，确保无缝连接不会闪黑
        public static void Destroy()
        {
            if (instance)
            {
                Destroy(instance.gameObject);
                instance = null;
            }
        }
    }
}
